// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <cantor/parser/parser.h>
#include <cantor/transform/ddls/constraint.h>
#include <cantor/transform/ddls/table_option.h>
#include <cantor/transform/ddls/index_option.h>
#include <cantor/transform/exprs/constant_expr.h>
#include <cantor/transform/exprs/range_var.h>
#include <cantor/transform/clause/column_name.h>
#include <cantor/transform/expr.h>


namespace cantor::transform {

    void TransConstraint::resolve_ast(const cantor::parser::Constraint*node) {
        if (!node) {
            status = turbo::data_loss_error("no expr");
            return;
        }

        constraint.set_name(node->name.to_string());
        constraint.set_type(node->type);
        constraint.set_index_dist(node->index_dist);

        if (node->index_option) {
            TransIndexOption option;
            option.resolve_ast(node->index_option);
            status = option.check();
            if (!status.ok()) {
                return;
            }
            *constraint.mutable_index_option() = std::move(option.index_option);
        }

        constraint.mutable_columns()->Reserve(node->columns.size());
        for (size_t i = 0; i < node->columns.size(); ++i) {
            auto out = constraint.mutable_columns()->Add();
            status = TransColumnName::resolve_to(node->columns[i], out);
            if (!status.ok()) {
                return;
            }
        }

        if (node->check_expr) {
            status = TransExpr::resolve_to(node->check_expr, constraint.mutable_check_expr());
            if (!status.ok()) {
                return;
            }
        }

        set_proto_location(node, constraint.mutable_location());
        set_proto_status(constraint.mutable_status());
    }

    const turbo::Status &TransConstraint::check() {
        if (!status.ok()) {
            return status;
        }
        if (!constraint.has_location()) {
            make_missing_required_field("location");
            return status;
        }
        return status;
    }
    turbo::Status TransConstraint::resolve_to(turbo::Nonnull<const cantor::parser::Constraint*> arg, turbo::Nonnull<pb::ast::Constraint *>out) {
        TransConstraint trans;
        trans.resolve_ast(arg);
        auto status = trans.check();
        if (!status.ok()) {
            return status;
        }
        *out = std::move(trans.constraint);
        return status;
    }
}  // namespace cantor::transform
